package com.multicert.hackathon.blmanager;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.json.JsonArray;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import com.multicert.hackathon.model.Result;

public class BaselineManagerImpl implements BaselineManager {

    /* Alfresco url for the Messias rest webservice */
    private URL alfrescoUrl;
    /* authentication parameters */
    private String username;
    private String password;
    /* http client and basic authentication */
    private final Client client;
    private final HttpAuthenticationFeature authFeature;

    public BaselineManagerImpl(URL alfrescoUrl, String username, String password) {
        this.alfrescoUrl = alfrescoUrl;
        this.username = username;
        this.password = password;
        this.client = ClientBuilder.newClient();
        this.authFeature = HttpAuthenticationFeature.universal(username, password);
        client.register(authFeature);
    }

    @Override
    public void createBaseline(String projectId, String name) throws Exception {

        Response result = client.target(alfrescoUrl.toURI())
            .path(projectId)
            .path(name)
            .request()
            .accept(MediaType.APPLICATION_JSON)
            .method(HttpMethod.POST);

        if (result.getStatus() != 200) {
            throw new BaselineException(result.getStatusInfo().getReasonPhrase());
        }
    }


    @Override
    public void updateBaseline(String projectId, String name) throws Exception {
        Result result = client.target(alfrescoUrl.toURI())
            .path(projectId)
            .path(name)
            .request()
            .accept(MediaType.APPLICATION_JSON)
            .method(HttpMethod.PUT, Result.class);

        if (result == null || !"OK".equals(result.getResult())) {
            throw new BaselineException(result == null ? "Null response!" : result.getResult());
        }
    }

    @Override
    public void deleteBaseline(String projectId, String name) throws Exception {
        Result result = client.target(alfrescoUrl.toURI())
            .path(projectId)
            .path(name)
            .request()
            .accept(MediaType.APPLICATION_JSON)
            .delete(Result.class);

        if (result == null || !"OK".equals(result.getResult())) {
            throw new BaselineException(result == null ? "Null response!" : result.getResult());
        }
    }

    @Override
    public List<String> getBaselines(String projectId) throws Exception {
        JsonArray jsonArray = client.target(alfrescoUrl.toURI())
            .path(projectId)
            .request(MediaType.APPLICATION_JSON)
            .get(JsonArray.class);

        if (jsonArray == null || jsonArray.size() == 0) {
            throw new BaselineException("Null response!");
        }

        List<String> baselines = new ArrayList<>(jsonArray.size());
        for (JsonValue value : jsonArray) {
            baselines.add(((JsonString) value).getString());
        }
        return baselines;
    }

}
